package com.zyx.config;

import com.zyx.interceptor.ContextHandlerInterceptor;
import com.zyx.resolver.CurrentUserMethodArgumentResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;

/**
 * <p>配置类，用于注册自定义参数解析器
 *
 * @author Yaxi.Zhang
 * @since 2022/10/7 10:41
 */
@Configuration
public class ArgumentResolverConfig implements WebMvcConfigurer {

    @Bean
    public CurrentUserMethodArgumentResolver getCurrentUserMethodArgumentResolver(){
        return new CurrentUserMethodArgumentResolver();
    }

    @Bean
    public HandlerInterceptor getHandlerInterceptor() {
        return new ContextHandlerInterceptor();
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        // 注册自定义参数解析器
        resolvers.add(getCurrentUserMethodArgumentResolver());
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册自定义拦截器
        registry.addInterceptor(getHandlerInterceptor());
    }
}
